//
//  AGDCAudioFrame.hpp
//  AgoraDemo
//
//  Created by tzx on 16/7/19.
//  Copyright © 2016年 apple. All rights reserved.
//

#ifndef AGDCAudioFrame_h
#define AGDCAudioFrame_h


#import <AudioToolbox/AudioToolbox.h>
#import <CoreAudio/CoreAudioTypes.h>

#include <iostream>
#import <AgoraRtcEngineKit/IAgoraMediaEngine.h>

#define kSampleRate 44100  //32000//

extern FILE *g_pcmFile;

extern FILE *g_pInFile;

//extern "C" int registerAudioFrameObserver(agora::media::IAudioFrameObserver *observer);

using AudioOutputCallback = std::function<void(uint8_t* data, size_t size, int inNumberFrames, bool isLocalAudio,void *context)>;


class CSouceAudioFrameObserver:public agora::media::IAudioFrameObserver

{
public:
    CSouceAudioFrameObserver();
    
    bool onRecordAudioFrame(AudioFrame& audioFrame);
    bool onPlaybackAudioFrame(AudioFrame& audioFrame);
    bool onPlaybackAudioFrameBeforeMixing(unsigned int uid, AudioFrame& audioFrame);
    
    void setCallback(AudioOutputCallback callback, void *context){m_callback = callback;m_context = context;}
    
    BOOL setupAudioUnitEX();
    const AudioUnit& audioUnit() const { return mAudioUnit; };
    
    void pushAudioFrame(unsigned char *inAudioFrame, int len);
    
    void startAudio();
    
    void stopAudio();
    
    void callCallback(uint8_t* Data, int len,int inNumberFrames );
    
protected:
    void setupAudioSession();
    
    
protected:
    
    NSString*                   _previousAVAudioSessionCategory;
    NSString*                   avAudioSessionMode;
    
    double                      avAudioSessionPreffSampleRate;
    NSInteger                   avAudioSessionChannels;
    
    AudioStreamBasicDescription	stream_format;
    
    
    AudioOutputCallback         m_callback;
    void                        *m_context;
    BOOL                        isPlayout;
    
    void*                       mAudioPool;
    
    AudioComponentInstance      mAudioUnit;
    AudioComponent              mComponent;


};

#endif /* AGDCAudioFrame_hpp */
